import 'package:flutter_test/flutter_test.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../lib/common/index.dart';
import '../lib/providers/index.dart';

void main() {
  group('Publish Functionality Tests', () {
    late ProviderContainer container;

    setUpAll(() async {
      // 初始化Hive（内存模式用于测试）
      await Hive.initFlutter();
      
      // 初始化服务
      await UserService.initialize();
      await ShareService.initialize();
    });

    setUp(() {
      container = ProviderContainer();
    });

    tearDown(() {
      container.dispose();
    });

    test('should create share successfully', () async {
      // 创建测试分享
      final share = ShareEntity(
        id: 'test_share_1',
        description: 'Test share description',
        image: 'test_image.jpg',
        emotion: 'happy',
        date: DateTime.now().toIso8601String(),
        commentIds: [],
        browses: 0,
        video: '',
      );

      // 测试创建分享
      final success = await ShareService.createShare(share);
      expect(success, true);

      // 验证分享是否被保存
      final savedShares = await ShareService.getSharesById('test_share_1');
      expect(savedShares.length, 1);
      expect(savedShares.first.description, 'Test share description');
      expect(savedShares.first.emotion, 'happy');
    });

    test('should create share with video', () async {
      // 创建带视频的测试分享
      final share = ShareEntity(
        id: 'test_share_video',
        description: 'Test video share',
        image: '',
        emotion: 'love',
        date: DateTime.now().toIso8601String(),
        commentIds: [],
        browses: 0,
        video: 'test_video.mp4',
      );

      // 测试创建分享
      final success = await ShareService.createShare(share);
      expect(success, true);

      // 验证分享是否被保存
      final savedShares = await ShareService.getSharesById('test_share_video');
      expect(savedShares.length, 1);
      expect(savedShares.first.video, 'test_video.mp4');
      expect(savedShares.first.hasVideo, true);
      expect(savedShares.first.hasImage, false);
    });

    test('should update user share list when creating share', () async {
      // 模拟登录用户
      final testUser = UserEntity(
        id: 'test_user',
        username: 'testuser',
        password: 'password',
        avatar: 'test_avatar.png',
        share: [],
        isVip: false,
        like: [],
      );

      // 保存测试用户
      await UserService.updateUser(testUser);
      
      // 创建分享
      final shareId = 'test_share_user';
      final share = ShareEntity(
        id: shareId,
        description: 'User test share',
        image: 'test.jpg',
        emotion: 'happy',
        date: DateTime.now().toIso8601String(),
        commentIds: [],
        browses: 0,
        video: '',
      );

      await ShareService.createShare(share);
      
      // 更新用户分享列表
      final updatedUser = testUser.copyWith(
        share: [...testUser.share, shareId],
      );
      await UserService.updateUser(updatedUser);

      // 验证用户分享列表是否更新
      final userShares = await ShareService.getSharesByUserId('test_user');
      expect(userShares.length, 1);
      expect(userShares.first.id, shareId);
    });

    test('should validate required fields for share creation', () async {
      // 测试情绪验证
      expect(() {
        ShareEntity(
          id: 'test',
          description: 'Test',
          image: '',
          emotion: '', // 空情绪应该被验证
          date: DateTime.now().toIso8601String(),
          commentIds: [],
          browses: 0,
          video: '',
        );
      }, returnsNormally); // ShareEntity构造函数不会抛出异常，验证应该在UI层

      // 测试描述验证
      expect(() {
        ShareEntity(
          id: 'test',
          description: '', // 空描述应该被验证
          image: 'test.jpg',
          emotion: 'happy',
          date: DateTime.now().toIso8601String(),
          commentIds: [],
          browses: 0,
          video: '',
        );
      }, returnsNormally); // ShareEntity构造函数不会抛出异常，验证应该在UI层
    });

    test('should handle share provider state correctly', () async {
      final shareNotifier = container.read(shareProvider.notifier);
      
      // 创建测试分享
      final share = ShareEntity(
        id: 'provider_test',
        description: 'Provider test share',
        image: 'test.jpg',
        emotion: 'happy',
        date: DateTime.now().toIso8601String(),
        commentIds: [],
        browses: 0,
        video: '',
      );

      // 测试通过provider创建分享
      final success = await shareNotifier.createShare(share);
      expect(success, true);

      // 验证状态是否更新
      final state = container.read(shareProvider);
      expect(state.shares.any((s) => s.id == 'provider_test'), true);
    });
  });
}
